﻿using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using SenseTimeTracker.BusinessLayer;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;

namespace SenseTimeTracker.Reports
{
	public class SaldoReportDisplayItem : CommonSenseControls.CommonSenseDisplayPanelItem
	{
		private DateTime date;
		private TimeSpan total = TimeSpan.Zero;
		private TimeSpan required = TimeSpan.Zero;
		private TimeSpan overtime = TimeSpan.Zero;
		private TimeSpan saldo = TimeSpan.Zero;

		private Dictionary<Guid, CategoryTotalValuePair> categoryTotals;

		public Dictionary<Guid, CategoryTotalValuePair> CategoryTotals
		{
			get { return categoryTotals; }
			set { categoryTotals = value; Height = (value.Count + 5) * 24; }
		}

		public DateTime Date
		{
			get { return date; }
			set { date = value; }
		}

		public TimeSpan Total
		{
			get { return total; }
			set { total = value; }
		}

		public TimeSpan Required
		{
			get { return required; }
			set { required = value; }
		}

		public TimeSpan Overtime
		{
			get { return overtime; }
			set { overtime = value; }
		}

		public TimeSpan Saldo
		{
			get { return saldo; }
			set { saldo = value; }
		}

		#region ISenseListItem Members

		public override void OnRender(Graphics g)
		{
			int index = 0;
			int rows = categoryTotals.Count + 5;
			DisplayInfo(g, index++, categoryTotals.Count + 2, date.ToString("MMMM yyyy", Persistance.Instance.SelectedLanguage), null);
			foreach (Guid key in categoryTotals.Keys)
			{
				CategoryTotalValuePair categoryTotalValuePair = categoryTotals[key];
				DisplayInfo(
					g,
					index++,
					rows,
					categoryTotalValuePair.TaskCategory.Id == DatabaseManager.UnknownTaskCategoryId ? Resources.UndefinedTaskCategory : categoryTotalValuePair.TaskCategory.Name,
					categoryTotalValuePair.CategoryTotal);
			}

			DisplayInfo(g, index++, rows, Resources.ReportTotalLabel, total);
			DisplayInfo(g, index++, rows, Resources.ReportRequiredLabel, required);
			DisplayInfo(g, index++, rows, Resources.ReportOvertimeLabel, overtime);
			DisplayInfo(g, index++, rows, Resources.ReportSaldoLabel, saldo);
			base.OnRender(g);
		}
		#endregion

		private void DisplayInfo(Graphics g, int line, int rows, string label, Nullable<TimeSpan> timeSpan)
		{
			using (Brush blackBrush = new SolidBrush(Color.Black))
			using (Brush redBrush = new SolidBrush(Color.Red))
			using (Font primaryFont = new Font("Helvetica Neue OTS", line == 0 ? 9f : 7f, line == 0 ? FontStyle.Bold : FontStyle.Regular))
			{
				SizeF stringSize = g.MeasureString(label, primaryFont);
				g.DrawString(label, primaryFont, blackBrush, ClientRectangle.Left + 8, ClientRectangle.Top - (line == 0 ? 8 : 0) + ClientRectangle.Height * (line * 2 + 1) / rows / 2 - stringSize.Height / 2);
				if (timeSpan.HasValue)
				{
					string stringTimeSpan = timeSpan.Value.TotalHours.ToString("0.00");
					stringSize = g.MeasureString(stringTimeSpan, primaryFont);
					g.DrawString(
						stringTimeSpan,
						primaryFont,
						timeSpan.Value.TotalHours >= 0 ? blackBrush : redBrush,
						ClientRectangle.Right - 8 - stringSize.Width,
						ClientRectangle.Top + ClientRectangle.Height * (line * 2 + 1) / rows / 2 - stringSize.Height / 2);
				}
			}
		}

		public class CategoryTotalValuePair
		{
			private Category taskCategory;
			private TimeSpan categoryTotal;

			public Category TaskCategory
			{
				get { return taskCategory; }
				set { taskCategory = value; }
			}

			public TimeSpan CategoryTotal
			{
				get { return categoryTotal; }
				set { categoryTotal = value; }
			}
		}
	}
}
